import Vue from "vue";
import Vuex from "vuex";

import modules from "./modules";
import { Message } from "element-ui";

import qs from "qs";
import {
	upLiveData,
	robotLiveData,
	upLiveCookie,
	robotLiveCookie,
} from "../utils/cookie";
import axios from "axios";

Vue.use(Vuex);
export default new Vuex.Store({
	state: {
		// 消息列表
		messages: [],
		// 消息最大数量
		maxMsgLength: 600,
		// 语音播报消息列表
		voices: [],
		// 正在播放的语音
		currentVoice: null,
		// 音乐列表
		musicList: [],
		// 正在播放的音乐
		currentMusic: null,
		// 自动回复消息队列
		autoReplyList: [],
		// 有无正在自动回复的消息
		currentautoReply: null,
	},
	mutations: {
		// 给列表添加新内容
		appendMessage(state, payload) {
			state.messages.push(payload);
			if (!!+localStorage.getItem("clearWelCome")) {
				// 如果开启清除前十秒的进入信息
				const newTime = Date.now() - 10000;
				state.messages = state.messages.filter(
					({ time, isEnter }) => !isEnter || time > newTime
				);
			}
			// 如果信息数量达到自己控制的上限就清空一半
			if (state.messages.length > state.maxMsgLength) {
				state.messages.splice(
					0,
					state.messages.length - state.maxMsgLength / 2
				);
			}
		},

		// 更换up清空消息
		clearMessages(state, payload) {
			state.messages = [];
			state.voices = [];
			state.musicList = [];
			state.autoReplyList = [];
		},

		// 添加语音消息列表
		appendVoices(state, payload) {
			if (
				localStorage.getItem("voiceBroadcast")
					? !!+localStorage.getItem("voiceBroadcast")
					: true
			) {
				if (state.voices.length || state.currentVoice) {
					state.voices.push(payload);
				} else {
					state.currentVoice = payload;
				}
			}
		},

		// 切换下一条语音
		nextVoice(state) {
			if (state.voices.length) {
				state.currentVoice = state.voices.shift();
			} else {
				state.currentVoice = null;
			}
		},

		// 添加音乐
		appendMusic(state, payload) {
			if (!state.musicList.length && !state.currentMusic) {
				state.currentMusic = payload;
			} else {
				state.musicList.push(payload);
			}
		},

		// 切换下一首音乐
		nextMusic(state, payload) {
			if (state.musicList.length) {
				state.currentMusic = state.musicList.shift();
			} else {
				state.currentMusic = null;
			}
		},

		// 添加自动回复消息
		appendAutoReply(state, payload) {
			if (!state.autoReplyList.length && !state.currentautoReply) {
				state.currentautoReply = payload;
			} else {
				state.autoReplyList.push(payload);
			}
		},

		// 下一条自动回复消息
		nextAutoReply(state) {
			if (state.autoReplyList.length) {
				state.currentautoReply = state.autoReplyList.shift();
			} else {
				state.currentautoReply = null;
			}
		},
	},
	actions: {
		// 发送信息的方法
		sendMsg(context, payload) {
			const { message, automaticReply } = payload;
			axios
				.post(
					"https://api.live.bilibili.com/msg/send",
					qs.stringify(
						automaticReply
							? {
									...robotLiveData(),
									msg: message,
							  }
							: {
									...upLiveData(),
									msg: message,
							  }
					),
					{
						headers: {
							myCookie: automaticReply
								? robotLiveCookie()
								: upLiveCookie(),
						},
						withCredentials: true,
					}
				)
				.then((res) => {
					if (res.data.code !== 0) {
						Message.error(res.data.message);
					}
					automaticReply && context.commit("nextAutoReply");
				});
		},
	},
	modules,
	strict: process.env.NODE_ENV !== "production",
});
